package com.totcy.baselibrary.utils;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * description: 日期事件处理
 * author: Totcy
 * date: 2017/8/10 14:20
 * update: 2017/8/10
 */

public class DateUtils {

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String YYYYMMDD_DOT = "yyyy.MM.dd";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String HH_MM = "HH:mm";
    //calendar 1开始
    public static final String weekArray[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五","星期六" };

    /**
     * 将字符串转为时间戳
     *
     * @param timeStr   时间字符串 2017-08-10 15:20:30
     * @param dateStyle 转化格式 yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static long getLongTime(String timeStr, String dateStyle) {
        long re_time = 0;
        SimpleDateFormat sdf = new SimpleDateFormat(dateStyle, Locale.CHINA);
        Date d;
        try {
            d = sdf.parse(timeStr);
            re_time = d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return re_time;
    }

    /**
     * // 将时间戳转为字符串
     *
     * @param cc_time   1502341954000
     * @param dateStyle yyyy年MM月dd日HH时mm分ss秒
     * @return
     */
    public static String getStrTime(String cc_time, String dateStyle) {
        String re_StrTime;
        SimpleDateFormat sdf = new SimpleDateFormat(dateStyle, Locale.CHINA);
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time));
        return re_StrTime;

    }
    /**
     * // 将时间戳转为字符串
     *
     * @param cc_time   1502341954000
     * @param dateStyle yyyy年MM月dd日HH时mm分ss秒
     * @return
     */
    public static String getStrTime(long cc_time, String dateStyle) {
        String re_StrTime;
        SimpleDateFormat sdf = new SimpleDateFormat(dateStyle, Locale.CHINA);
        re_StrTime = sdf.format(new Date(cc_time));
        return re_StrTime;
    }

    /**
     * 根据 timestamp 生成各类时间状态串 今天的返回 HH:mm ，反之返回 YYY-MM-DD
     *
     * @param timestamp 距1970 00:00:00 GMT的秒数
     * @return 时间状态串
     */
    @SuppressLint("SimpleDateFormat")
    public static String getTimeState(String timestamp) {

        if (timestamp == null || "".equals(timestamp)) {
            return "";
        }
        try {
            timestamp = formatTimestamp(timestamp);
            long _timestamp = Long.parseLong(timestamp);

            Calendar now = Calendar.getInstance();
            Calendar c = Calendar.getInstance();
            c.setTimeInMillis(_timestamp);

            if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)
                    && c.get(Calendar.MONTH) == now.get(Calendar.MONTH)
                    && c.get(Calendar.DATE) == now.get(Calendar.DATE)) {
                SimpleDateFormat sdf = new SimpleDateFormat(HH_MM);
                return sdf.format(c.getTime());
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
                return sdf.format(c.getTime());
            }

        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 对时间戳格式进行格式化，保证时间戳长度为13位
     *
     * @param timestamp 时间戳
     * @return 返回为13位的时间戳
     */
    private static String formatTimestamp(String timestamp) {
        if (timestamp == null || "".equals(timestamp)) {
            return "";
        }
        String tempTimeStamp = timestamp + "00000000000000";
        StringBuilder sb = new StringBuilder(tempTimeStamp);
        return  sb.substring(0, 13);
    }

}
